package org.wcms.surrounded.persistence;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.wcms.surrounded.model.Record;
import org.wcms.surrounded.model.User;

public class HighScoresService {

    private final RecordService recordService;
    private final UserService userService;
    
    @Inject
    public HighScoresService(RecordService recordService, UserService userService) {
        this.userService = userService;
        this.recordService = recordService;
    }
    
    public Map<User, List<Integer>> getHighScores() {
        
        List<Record> records = recordService.getTopTenRecords();

        List<Long> ids = new ArrayList<Long>();
        for (Record record : records) {
            ids.add(record.getUserId());
        }

        Map<Long, User> users = userService.getUsersByIds(ids);
        
        Map<User, List<Integer>> result = new HashMap<User ,List<Integer>>();
        
        for (Record record : records) {
            User user = users.get(record.getUserId());
            
            if (!result.containsKey(user)) {
                result.put(user, new ArrayList<Integer>());
            }
            
            result.get(user).add(record.getScore());
        }
        
        return result;
    }
    
}
